package com.zzyl.nursing.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.nursing.domain.NursingTask;
import com.zzyl.nursing.dto.NursingTaskDto;
import com.zzyl.nursing.mapper.NursingTaskMapper;
import com.zzyl.nursing.service.INursingTaskService;
import com.zzyl.nursing.vo.NursingTaskVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 护理任务Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-04
 */
@Service
public class NursingTaskServiceImpl extends ServiceImpl<NursingTaskMapper, NursingTask> implements INursingTaskService
{
    @Autowired
    private NursingTaskMapper nursingTaskMapper;

    /**
     * 查询护理任务
     * 
     * @param id 护理任务主键
     * @return 护理任务
     */
    @Override
    public NursingTask selectNursingTaskById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询护理任务列表
     * 
     * @param nursingTaskDto 护理任务
     * @return 护理任务
     */
    @Override
    public List<NursingTask> selectNursingTaskList(NursingTaskDto nursingTaskDto)
    {
        return nursingTaskMapper.selectNursingTaskList(nursingTaskDto);
    }

    /**
     * 新增护理任务
     * 
     * @param nursingTask 护理任务
     * @return 结果
     */
    @Override
    public int insertNursingTask(NursingTask nursingTask)
    {
        return save(nursingTask) ? 1 : 0;
    }

    /**
     * 修改护理任务
     * 
     * @param nursingTask 护理任务
     * @return 结果
     */
    @Override
    public int updateNursingTask(NursingTask nursingTask)
    {
        return updateById(nursingTask) ? 1 : 0;
    }

    /**
     * 批量删除护理任务
     * 
     * @param ids 需要删除的护理任务主键
     * @return 结果
     */
    @Override
    public int deleteNursingTaskByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理任务信息
     * 
     * @param id 护理任务主键
     * @return 结果
     */
    @Override
    public int deleteNursingTaskById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    @Override
    public List<NursingTaskVo> selectNursingTaskVoList(NursingTaskDto nursingTaskDto) {
        List<NursingTask> nursingTasks = nursingTaskMapper.selectNursingTaskVoList(nursingTaskDto);
        if (nursingTasks == null) {
            return List.of();
        }
        List<NursingTaskVo> nursingTaskVos = new ArrayList<>();
        for (NursingTask nursingTask : nursingTasks) {
            NursingTaskVo vo = new NursingTaskVo();
            vo.setId(nursingTask.getId());
            vo.setNursingId(nursingTask.getNursingId());
            vo.setProjectId(nursingTask.getProjectId());
            vo.setProjectName(nursingTask.getProjectName());
            vo.setElderId(nursingTask.getElderId());
            vo.setElderName(nursingTask.getElderName());
            vo.setBedNumber(nursingTask.getBedNumber());
            vo.setEstimatedServerTime(nursingTask.getEstimatedServerTime());
            // 类型转换：将 Date 转换为 LocalDateTime
            if (nursingTask.getCreateTime() != null) {
                vo.setCreateTime(nursingTask.getCreateTime().toInstant()
                        .atZone(ZoneId.systemDefault())
                        .toLocalDateTime());
            }
            vo.setStatus(nursingTask.getStatus());
            // nursingName 字段需要额外处理，暂时留空
            nursingTaskVos.add(vo);
        }
        return nursingTaskVos;
    }
}
